package cn.ssdcc.dkserver.controller;

import cn.ssdcc.dkserver.entity.DkPath;
import cn.ssdcc.dkserver.entity.DkResponse;
import cn.ssdcc.dkserver.entity.Music;
import cn.ssdcc.dkserver.entity.User;
import cn.ssdcc.dkserver.exception.BusinessException;
import cn.ssdcc.dkserver.util.IsNullUtil;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/UserApi")
@Slf4j
public class UserController extends CommonController{

    @RequestMapping(value = "/UserLo", method = RequestMethod.POST)
    @ResponseBody
    public DkResponse<User> UserLo(@RequestBody User user) {
        User reuser = userBiz.getUserMsgByDeviceid(user.getDeviceid());
        return new DkResponse<User>(reuser);
    }

    @RequestMapping(value = "/UpdateUser", method = RequestMethod.POST)
    @ResponseBody
    public DkResponse<User> UpdateUser(@RequestBody User user) {
        User reuser = userBiz.getUserMsgByDeviceid(user.getDeviceid());
        user.setId(reuser.getId());
        log.info(JSON.toJSONString(user));
        userMapper.updateUser(user);
        reuser = userBiz.getUserMsgByDeviceid(user.getDeviceid());
        return new DkResponse<User>(reuser);
    }

    @RequestMapping("/UpUserImg")
    @ResponseBody
    public DkResponse<String> UpUserImg(@RequestParam("file") MultipartFile file){
        log.info("文件名："+file.getOriginalFilename());
        String imgpath = userBiz.buildPath("/user/",file.getOriginalFilename());
        log.info(imgpath);
        upLoadBiz.saveMultipartFile(file, DkPath.DataPath+imgpath);
        DkResponse<String> dkResponse = new DkResponse<>();
        dkResponse.setData(imgpath);
        return dkResponse;
    }
    private static HashMap<String,String> userpswmap = new HashMap<>();
    @RequestMapping("/SetOtherDevice")
    @ResponseBody
    public DkResponse<User> SetOtherDevice(@RequestBody User user){
        String deviceid = userpswmap.get(user.getId());
        DkResponse dkResponse = new DkResponse();
        if (IsNullUtil.IsNullString(deviceid)){
            throw new BusinessException("登陆失败");
        }
        userMapper.deleteThirddevice(user.getDeviceid());
        userMapper.insertThirddevice(deviceid,user.getDeviceid());
        User reuser = userBiz.getUserMsgByDeviceid(user.getDeviceid());
        dkResponse.setData(reuser);
        userpswmap.remove(user.getId());
        log.info(JSON.toJSONString(userpswmap));
        return dkResponse;
    }

    @RequestMapping("/SetPsw")
    @ResponseBody
    public DkResponse SetPsw(@RequestBody User user){
        User reuser = userBiz.getUserMsgByDeviceid(user.getDeviceid());
        userpswmap.put(user.getId(),reuser.getDeviceid());
        log.info(JSON.toJSONString(userpswmap));
        return new DkResponse();
    }

}
